package com.xpgk.mvc.collection.collectionServicePart;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.jfinal.log.Log;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.bentryRegister.Customer;
import com.xpgk.mvc.binShopService.ServicePartSize;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.productSet.Product;

public class CollecttionBinShopServiceController extends BaseController {
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(CollecttionBinShopServiceController.class);


	public void index() {
		String flag  = getPara("flag");
		setAttr("flag", flag);
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("column", Product.column_typeIds);
		String sql = getSqlByBeetl(Product.sqlId_column, param);
		setAttr("plaster", Product.dao.find(sql, Code.proYt_ids));
		setAttr("organids", getCUserOrganIds());
		setAttr("date", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		String customerIds = getPara("customerIds");
		if(StringUtils.isNotBlank(customerIds)){
			Customer cus = Customer.dao.findById(customerIds);
			setAttr("customer", cus);
		}
		render("/collection/servicePart/collectionServicePart.html");
	}



	public void savePart() throws ParseException {
		String sqlSize = "SELECT * FROM sb_customer_service_partsize where customerIds = ? and DATE_FORMAT(createDate, '%Y-%m-%d')  = ? and dataStatus in ('1','2')";
		
		ServicePartSize  spz = getModel(ServicePartSize.class,"servicePartSize");
		//CollecttionServicePartSize spz = new CollecttionServicePartSize();
		String date = getPara("temporary.gxsj");
	    SimpleDateFormat d=new SimpleDateFormat("yyyy-MM-dd");
	    Date date2 = d.parse(date); 
		
		ServicePartSize servicePartSize = ServicePartSize.dao.findFirst(sqlSize,spz.getStr("customerIds"),date);
		if(servicePartSize!=null){
			 if( spz.getStr(ServicePartSize.column_qs)!=null && !spz.getStr(ServicePartSize.column_qs).equals("")){
				 servicePartSize.set(ServicePartSize.column_qs, spz.getStr(ServicePartSize.column_qs));
			 }else {
				 servicePartSize.set(ServicePartSize.column_qs, "");
			}
			 
			 if( spz.getStr(ServicePartSize.column_qz)!=null && !spz.getStr(ServicePartSize.column_qz).equals("")){
				 servicePartSize.set(ServicePartSize.column_qz, spz.getStr(ServicePartSize.column_qz));
			 }else {
				 servicePartSize.set(ServicePartSize.column_qz, "");
			}
			 
			 if( spz.getStr(ServicePartSize.column_qx)!=null && !spz.getStr(ServicePartSize.column_qx).equals("")){
				 servicePartSize.set(ServicePartSize.column_qx, spz.getStr(ServicePartSize.column_qx));
			 }else {
				 servicePartSize.set(ServicePartSize.column_qx,"");
			}
			 
			 if( spz.getStr(ServicePartSize.column_zb)!=null && !spz.getStr(ServicePartSize.column_zb).equals("")){
				 servicePartSize.set(ServicePartSize.column_zb, spz.getStr(ServicePartSize.column_zb));
			 }else {
				 servicePartSize.set(ServicePartSize.column_zb, "");
			}
			 
			 if( spz.getStr(ServicePartSize.column_yb)!=null && !spz.getStr(ServicePartSize.column_yb).equals("")){
				 servicePartSize.set(ServicePartSize.column_yb, spz.getStr(ServicePartSize.column_yb));
			 }else {
				 servicePartSize.set(ServicePartSize.column_yb,"");
			}
			 
			 if( spz.getStr(ServicePartSize.column_zdt)!=null && !spz.getStr(ServicePartSize.column_zdt).equals("")){
				 servicePartSize.set(ServicePartSize.column_zdt, spz.getStr(ServicePartSize.column_zdt));
			 }else {
				 servicePartSize.set(ServicePartSize.column_zdt, "");
			}
			 
			 if( spz.getStr(ServicePartSize.column_ydt)!=null && !spz.getStr(ServicePartSize.column_ydt).equals("")){
				 servicePartSize.set(ServicePartSize.column_ydt, spz.getStr(ServicePartSize.column_ydt));
			 }else {
				 servicePartSize.set(ServicePartSize.column_ydt,"");
			}
			 
			 if( spz.getStr(ServicePartSize.column_yxt)!=null && !spz.getStr(ServicePartSize.column_yxt).equals("")){
				 servicePartSize.set(ServicePartSize.column_yxt, spz.getStr(ServicePartSize.column_yxt));
			 }else {
				 servicePartSize.set(ServicePartSize.column_yxt, "");
			}
			 
			 if( spz.getStr(ServicePartSize.column_zxt)!=null && !spz.getStr(ServicePartSize.column_zxt).equals("")){
				 servicePartSize.set(ServicePartSize.column_zxt, spz.getStr(ServicePartSize.column_zxt));
			 }else {
				 servicePartSize.set(ServicePartSize.column_zxt, "");
			}
			
			 servicePartSize.set(ServicePartSize.column_updateDate, new Date())
			 .set(ServicePartSize.column_updateId, getCUserIds());
			 servicePartSize.update();
		}else{
			spz.set(ServicePartSize.column_dataStatus, 2);
			spz.set("createId", getCUserIds());
			spz.set("createDate",date2);
			spz.save();
		}
		
		renderText("200");
	}
	
	


}
